﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.DirectoryServices.ActiveDirectory;

using ADSync2.ActiveDirectory;
using ADSync2.ConfigurationComponent;
using ADSync2.EngineComponents;

namespace ADSync2
{
	/// <summary>
	/// Main execution class; Entry point for project;
	/// </summary>
	public class Engine : IDisposable
	{
		#region Constants

		#endregion

		#region Variables
		private bool bDisposed;
		#endregion

		#region Properties
		private EngineConfiguration Configuration
		{
			get;
			set;
		}
		
		#endregion

		#region Constructors
		public Engine(EngineConfiguration Configuration)
		{
			this.Configuration = Configuration;
		}
		#endregion

		#region Public Methods
		public void Start()
		{
			SyncHistory SyncStateForSyncHistory = new SyncHistory();

			Operations.ExecutionModes ExecutionMode = Operations.ExecutionModes.NoCommit;

			switch (this.Configuration.KeyValue["MODE"])
			{
				case "NORMAL": ExecutionMode ^= Operations.ExecutionModes.NoCommit;
					break;
			}

			if (this.Configuration.KeyValue.ContainsKey("SYNC_TYPE"))
			{
				switch (this.Configuration.KeyValue["SYNC_TYPE"])
				{
					case "FULL_SYNC": ExecutionMode |= Operations.ExecutionModes.FullSync;
						break;
					case "DELTA_SYNC":;
						break;
				}
			}

			iLog.LoggingModes LoggingMode = 0x00;
			String loggingMode = this.Configuration.KeyValue["LOG"].Split(':')[0];
			String loggingMedia = this.Configuration.KeyValue["LOG"].Split(':')[1];
			switch (loggingMode)
			{
				case "VERBOSE": LoggingMode ^= iLog.LoggingModes.Verbose;
					break;
				//case "QUIET": LoggingMode = iLog.LoggingModes.NormalWithFile;
				//	break;
			}

			foreach (String logTo in loggingMedia.Split(','))
			{
				switch (logTo)
				{
					case "FILE": LoggingMode ^= iLog.LoggingModes.File;
						break;
					case "SYSLOG": LoggingMode ^= iLog.LoggingModes.SystemLog;
						break;
					case "CLI": LoggingMode ^= iLog.LoggingModes.Console;
						break;
				}
			}

			using (Operations ActiveDirectoryOperations = new Operations(ExecutionMode, LoggingMode))
			{
				ActiveDirectoryOperations.ExecuteJobsLegacyBased(this.Configuration.KeyValue);
			}
		}
		#endregion
	
		#region Private Methods

		#endregion

		#region IDisposable Members
		public void Dispose()
		{
			Dispose(true);
			GC.SuppressFinalize(this);
		}

		protected virtual void Dispose(bool bDisposing)
		{
			if (!this.bDisposed)
			{
				if (bDisposing)
				{
				}

				this.bDisposed = true;
			}
		}

		#endregion
	}
}
